<?php
////////////////////////////////////////////////////////////////////////////////
//	  This file is part of PushLMS.
//
//    PushLMS is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    PushLMS is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with PushLMS.  If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////
	
class User extends AppModel {

	var $name = 'User';
	var $recursive = 2;
	var $displayField = 'username';
	
	var $belongsTo = array(
		'Usergroup',
	);

	var $validate = array(

		'firstname' => array(
			'notEmpty' => array(
				'rule' => 'notEmpty',
				'message' => 'Firstname cannot be blank.'
			)
		),

		'lastname' => array(
			'notEmpty' => array(
				'rule' => 'notEmpty',
				'message' => 'Lastname cannot be blank.'
			)
		),

		'email' => array(
			'email' => array(
				'rule' => 'email',
				'message' => 'Invalid email address must be <user@domain.com>.'
			),
			'isUnique' => array(
				'rule' => 'isUnique',
				'message' => 'The email address supplied is already registered.'
			),
			'notEmpty' => array(
				'rule' => 'notEmpty',
				'message' => 'Email cannot be blank.'
			),
		),

		'username' => array(
			'alphaNumeric' => array(
				'rule' => 'alphaNumeric',
				'message' => 'Username must be alpha numeric.'
			),
			'isUnique' => array(
				'rule' => 'isUnique',
				'message' => 'The username supplied is already registered.'
			),
			'notEmpty' => array(
				'rule' => 'notEmpty',
				'message' => 'Username cannot be blank.'
			)
		),

		'password' => array(
			'notEmpty' => array(
				'rule' => 'notEmpty',
				'message' => 'Password cannot be blank.'
			),
			#'minLength' => array(
			#	'rule' => array('minLength', '8'),
			#	'message' => 'Password has a mimimum 8 characters long'
			#),
		),

	);

	function beforeSave() {
		if(isset($this->data[$this->alias]['password'])){
			$this->data[$this->alias]['password'] = Security::hash($this->data[$this->alias]['password'], null, true);
		}
		return true;
	}

	function getActivationHash()
	{
		if (!isset($this->id)) {
			return false;
		}
		return md5(rand());

	}

	function getRandomPassword()
	{
		if (!isset($this->id)) {
			return false;
		}
		return substr(md5(rand()), 0, 10);
	}

}
?>
